package com.wh.hotel.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.common.annotation.ResultLog;
import com.wh.common.enums.MethodTypeEnum;
import com.wh.hotel.constants.CommonConstant;
import com.wh.hotel.enums.HotelStatusEnum;
import com.wh.hotel.enums.base.HotelOperateTypeEnum;
import com.wh.hotel.mapper.HotelBaseInfoMapper;
import com.wh.hotel.model.HotelBaseInfo;
import com.wh.hotel.vo.HotelQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luweije
 */
@Component
public class HotelBaseInfoDao {

    @Resource
    private HotelBaseInfoMapper hotelBaseInfoMapper;

    @ResultLog(name = "HotelBaseInfoDao.insert", methodType = MethodTypeEnum.DB)
    public boolean insert(HotelBaseInfo hotelBaseInfo) {
        if (hotelBaseInfo == null) {
            return false;
        }
        return hotelBaseInfoMapper.insert(hotelBaseInfo) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelBaseInfoDao.operateHotelStatus", methodType = MethodTypeEnum.DB)
    public boolean operateHotelStatus(Long id, HotelOperateTypeEnum operateTypeEnum) {
        HotelBaseInfo updateModel = new HotelBaseInfo();
        LambdaQueryWrapper<HotelBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelBaseInfo::getId, id);
        switch (operateTypeEnum) {
            case ON:
                queryWrapper.in(HotelBaseInfo::getHotelStatus, HotelStatusEnum.OFFLINE.getCode());
                updateModel.setHotelStatus(HotelStatusEnum.NORMAL.getCode());
                break;
            case OFF:
                queryWrapper.eq(HotelBaseInfo::getHotelStatus, HotelStatusEnum.NORMAL.getCode());
                updateModel.setHotelStatus(HotelStatusEnum.OFFLINE.getCode());
                break;
        }
        return hotelBaseInfoMapper.update(updateModel, queryWrapper) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelBaseInfoDao.updateById", methodType = MethodTypeEnum.DB)
    public boolean updateById(HotelBaseInfo hotelBaseInfo) {
        return hotelBaseInfoMapper.updateById(hotelBaseInfo) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelBaseInfoDao.queryPageByCondition", methodType = MethodTypeEnum.DB, printResult = false)
    public IPage<HotelBaseInfo> queryPageByCondition(HotelQueryVo hotelQueryVo) {
        IPage<HotelBaseInfo> page = new Page<>(hotelQueryVo.getPageNum(), hotelQueryVo.getPageSize());
        LambdaQueryWrapper<HotelBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(hotelQueryVo.getId() != null, HotelBaseInfo::getId, hotelQueryVo.getId());
        queryWrapper.likeRight(StringUtils.isNotBlank(hotelQueryVo.getHotelName()), HotelBaseInfo::getHotelName, hotelQueryVo.getHotelName());
        queryWrapper.orderByDesc(HotelBaseInfo::getUpdateDt);
        return hotelBaseInfoMapper.selectPage(page, queryWrapper);
    }

    @ResultLog(name = "HotelBaseInfoDao.queryPageByCondition", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelBaseInfo> queryPageById(long id, int pageSize) {
        LambdaQueryWrapper<HotelBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(HotelBaseInfo::getId, id);
        queryWrapper.orderByAsc(HotelBaseInfo::getId);
        queryWrapper.last(" limit " + pageSize);
        return hotelBaseInfoMapper.selectList(queryWrapper);
    }

    @ResultLog(name = "HotelBaseInfoDao.selectBatchById", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelBaseInfo> selectBatchById(List<Long> hotelIdList) {
        return hotelBaseInfoMapper.selectBatchIds(hotelIdList);
    }

    @ResultLog(name = "HotelBaseInfoDao.selectById", methodType = MethodTypeEnum.DB, printResult = false)
    public HotelBaseInfo selectById(Long hotelId) {
        return hotelBaseInfoMapper.selectById(hotelId);
    }
}
